﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AggregateDemo
{
    public class Program
    {
        static void Main(string[] args)
        {
            Sample();
            Linq93();
            Console.WriteLine("The result of generic function : {0}",
                              GenericFuntion("abc", m => (m == "abc") ? "def" : "abc"));
            Console.ReadLine();
        }

        public static void Sample()
        {
            double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };

            var result = doubles.Aggregate(100.0, (reslt, item) => reslt + item);

            Console.WriteLine("Ket qua:" + result);
        }

        public static void Linq93()
        {
            var startBalance = 100.0;

            int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };

            double endBalance =
                attemptedWithdrawals.Aggregate(startBalance,
                    (balance, nextWithdrawal) =>
                        ((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));

            Console.WriteLine("Ending balance: {0}", endBalance);
        }

        public static string GenericFuntion(string text, Func<string, string> func)
        {
            return func(text);
        }
    }
}
